﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using digit.Core;
using SMS.ServiceManager;
using SMS.UserManager;

public partial class Modules_ListServiceDetail : WebControlBase
{
    private int groupCd;

    protected void Page_Load(object sender, EventArgs e)
    {
        groupCd = Request.QueryString["gcd"] != null ? Int32.Parse(Request.QueryString["gcd"].ToString().Trim()) : -1;

        if (!ClientState.IsAuthenticated)
        {
            Response.Redirect("~/Login.aspx?session=timeout");
        }

        this.lnkDelete.Attributes.Add("onclick", "return confirm('Bạn đã chắc chắn xóa?');");
        this.lnkDeleteBottom.Attributes.Add("onclick", "return confirm('Bạn đã chắc chắn xóa?');");

        if (ClientState.MemberInfo.UserType != 0 && ClientState.MemberInfo.UserType != 1 && ClientState.MemberInfo.UserType != 2)
        {
            lnkAdd.Enabled = false;
            lnkAddBottom.Enabled = false;
            lnkDelete.Enabled = false;
            lnkDeleteBottom.Enabled = false;
        }
        else
        {
            lnkAdd.Enabled = true;
            lnkAddBottom.Enabled = true;
            lnkDelete.Enabled = true;
            lnkDeleteBottom.Enabled = true;
        }

        if (IsPostBack) return;

        this.BindGroup();
        this.BindData(groupCd);
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        BindData(groupCd);
    }

    private void BindGroup()
    {
        this.ddlGroup.DataSource = CodeManager.GetCodeDetailTable("SERVICE", "SERVICE_GROUP");
        this.ddlGroup.DataBind();
        this.ddlGroup.Items.Insert(0, new ListItem("-- All --", "0"));
    }

    protected void lnkAdd_Click(object sender, EventArgs e)
    {
        Response.Redirect("Default.aspx?module=DetailServiceDetail");
    }

    protected void lnkDelete_Click(object sender, EventArgs e)
    {
        int iDeleted = 0;
        ActivitiesInfo objActive = new ActivitiesInfo();

        //Search all checked rows to delete
        foreach (GridViewRow r in this.grvServiceDetail.Rows)
        {
            //If checkbox is not checked then quit
            HtmlInputCheckBox cbx = (HtmlInputCheckBox)r.Cells[0].Controls[1];
            if (!cbx.Checked) continue;

            Label lbl = (Label)r.FindControl("lblCmd");
            if (lbl != null)
            {
                objActive.TableName = lbl.Text;
                objActive.DoContent = "Delete:" + lbl.Text + ",By:" + ClientState.MemberInfo.Username;
            }

            //If checkbox is checked then delete
            ServiceDetailsInfo obj = ServiceDetailsManager.Select(Util.ToInteger(cbx.Value));

            if (obj != null)
            {
                if (ClientState.MemberInfo.VerifyCode.Contains(obj.Cmd1.ToLower() + ",") || ClientState.MemberInfo.Username == "admin" || ClientState.MemberInfo.UserType == (byte)0)
                {
                    if (ServiceDetailsManager.Delete(Util.ToInteger(cbx.Value)))
                    {
                        iDeleted++;

                        // Add log
                        objActive.Action = "Delete - SD";
                        objActive.ActivityDate = DateTime.Now;
                        objActive.UserID = ClientState.MemberInfo.MemberID;

                        ActivitiesManager.Insert(objActive);
                    }
                }
            }
        }

        //Display result of delete process
        if (iDeleted == 0)
        {
            this.lblError.Text = "Không có bản ghi nào được xóa!";
            return;
        }

        this.lblError.Text = "Đã xóa " + iDeleted.ToString() + " bản ghi!";

        this.BindData(groupCd);
    }

    protected void grvServiceDetail_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.grvServiceDetail.PageIndex = e.NewPageIndex;
        this.BindData(groupCd);
    }

    private void BindData(int gCd)
    {
        try
        {
            string strFilter = "";
            DataTable dt = ServiceDetailsManager.SelectAll();

            if (dt != null && dt.Rows.Count > 0)
            {
                DataView dv = dt.DefaultView;

                if (this.txtCode.Text != "") strFilter += "Cmd1 LIKE '%" + this.txtCode.Text.Trim() + "%' OR Param1 LIKE '" + this.txtCode.Text.Trim() + "%'";

                if (gCd != -1) 
                { 
                    strFilter += "GroupCd = " + gCd.ToString();
                    this.ddlGroup.Enabled = false;
                }
                else
                    if (this.ddlGroup.SelectedValue != "0") strFilter += "GroupCd = " + this.ddlGroup.SelectedValue;

                dv.RowFilter = strFilter;

                dv.Sort = "CreatedDate DESC";

                this.grvServiceDetail.DataSource = dv;
                this.grvServiceDetail.DataBind();
                this.lblError.Text = "Hệ thống có tổng số <b>" + dv.ToTable().Rows.Count + "</b> dịch vụ đang chạy";
            }
            else
            {
                this.lblError.Text = "Hiện tại chưa có dịch vụ nào được cập nhật";
            }
        }
        catch { }
    }
}
